<?php
/******************************************************************************
 *
 *    OpenZoowa - Web App to manage events
 *    Copyright (C) 2008 Jesus Ruiz-Ayucar
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/
   if (!defined('ZOOWA')) {
      include_once('../../config.php');
   }
   class Country {
      private $id;
      private $name;
      private $name_clean;

      public function Country($id = null) {
         if (isset($id) && Util::is_numeric($id) && $id > 0) {
            global $db;
            $SQL  = 'SELECT * FROM '.$GLOBALS["db_pre"].'countries WHERE ';
            $SQL .= 'id = ' . $db->escape($id);
            $country = $db->get_row($SQL);
            $this->id = $country->id;
            $this->name = $country->name;
            $this->name_clean = $country->name_clean;
         }
      }

      public function setName($name) {
         if (!empty($name) && strlen($name) <= 128) {
            $this->name = $name;
         }
      }

      public function setNameClean($name_clean) {
         if (!empty($name_clean) && strlen($name_clean) <= 128) {
            $this->name_clean = $name_clean;
         }
      }

      public function add() {
         if (!isset($this->id) && !empty($this->name) && !empty($this->name_clean)) {
            global $db;
            $SQL  = 'INSERT INTO '.$GLOBALS["db_pre"].'countries ';
            $SQL .= '(name, name_clean) VALUES (';
            $SQL .= '"'.$db->escape($this->name).'",';
            $SQL .= '"'.$db->escape($this->name_clean).'")';
            $db->query($SQL);
            $this->id = $db->insert_id;
            return $this->id;
         }
      }

      public function del() {
         if (isset($this->id)) {
            global $db;

            $SQL  = 'DELETE FROM '.$GLOBALS["db_pre"].'countries WHERE ';
            $SQL .= 'id = '.$db->escape($this->id);
            if($db->query($SQL)) {
               $this->name = null;
               $this->name_clean = null;
               $this->id = null;
            }
         }
      }

      public function getId() {
         return $this->id;
      }

      public function getName() {
         return $this->name;
      }

      public function getNameClean() {
         return $this->name_clean;
      }



/**
*	HELPERS
*/
      // Returns all countries
      public static function getList() {
         global $db;

         $SQL = 'SELECT id FROM '.$GLOBALS["db_pre"].'countries';
         foreach($db->get_col($SQL) as $country) {
            $countries[] = new Country($country);
         }
         return $countries;
      }

      public static function getIdFromClean($clean_name) {
         global $db;
         $SQL  = 'SELECT id FROM '.$GLOBALS["db_pre"].'countries ';
         $SQL .= 'WHERE name_clean = "'.$db->escape($clean_name).'"';
         return $db->get_var($SQL);
      }
   }
?>